package main.model.hexisland.location;

/**
 * Co-ordinates for a hexagon location.
 * Uses both Q,R co-ordinates and X,Y,Z.
 * Either can be used for construction and either can be got.
 *
 * User: gabe
 * Date: 10/06/13
 */
public class HexLocation {


    private Integer x = null, y = null ,z = null;
    private Integer q = null, r = null;

    public HexLocation(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public HexLocation(int q, int r){
        this.q = q;
        this.r = r;
    }

    public HexLocation(HexLocation hexLocation){
        this.q = hexLocation.getQ();
        this.r = hexLocation.getR();
    }

    private void setLocation(int x, int y, int z){
        this.q = x + (z + z&1) / 2;
        this.r = z;
    }

    private void setLocation(int q, int r){
        this.x = q - (r + r&1) / 2;
        this.z = r;
        this.y = -x-z;
    }

    public int getX() {
        if(x == null) setLocation(q, r);
        return x;
    }

    public int getY() {
        if(y == null) setLocation(q, r);
        return y;
    }

    public int getZ() {
        if(z == null) setLocation(q, r);
        return z;
    }

    public int getQ() {
        if(q == null) setLocation(x, y, z);
        return q;
    }

    public int getR() {
        if(r == null) setLocation(x, y, z);
        return r;
    }

    @Override
    public int hashCode() {
        int hashCode = 3;
        hashCode = 5  * hashCode + getQ();
        hashCode = 13 * hashCode + getR();
        return hashCode;
    }

    @Override
    public boolean equals(Object obj) {
        if ( this == obj ) return true;
        if ( !(obj instanceof HexLocation) ) return false;

        HexLocation hexLocation = (HexLocation)obj;
        if(hexLocation.getQ() == this.getQ()
                && hexLocation.getR() == this.getR()){
            return true;
        }
        else return false;
    }

    @Override
    public String toString() {
        return "Q:"+getQ()+",R:"+getR();
    }
}
